<template>
    <div class="sidebar">
        <el-menu
            class="sidebar-el-menu"
            :default-active="onRoutes"
            background-color="#ffffff"
            text-color="#5b6164"
            active-text-color="#14a7f1"
            unique-opened
            router
            :collapse="sidebar.collapse"
        >
            <template v-for="item in items">
                <template v-if="item.children">
                    <el-sub-menu :index="item.path" :key="item.path">
                        <template #title>
                            <el-icon>
                                <component :is="item.icon"></component>
                            </el-icon>
                            <span>{{ item.title }}</span>
                        </template>
                        <template v-for="subItem in item.children">
                            <el-sub-menu
                                v-if="subItem.children"
                                :index="subItem.path"
                                :key="subItem.path"
                            >

                                <template #title>
                                  <el-icon>
                                    <component :is="item.icon"></component>
                                  </el-icon>
                                  <span>
                                    {{ subItem.title }}
                                  </span>
                                </template>
                                <el-menu-item v-for="(threeItem, i) in subItem.children" :key="i" :index="threeItem.path">
                                    {{ threeItem.title }}
                                </el-menu-item>
                            </el-sub-menu>
                            <el-menu-item v-else :index="subItem.path">
                              <template #title>
                                <el-icon>
                                  <component :is="subItem.icon"></component>
                                </el-icon>
                                <span>
                                    {{ subItem.title }}
                                  </span>
                              </template>
                            </el-menu-item>
                        </template>
                    </el-sub-menu>
                </template>
                <template v-else>
                    <el-menu-item :index="item.path" :key="item.path">
                        <el-icon>
                            <component :is="item.icon"></component>
                        </el-icon>
                        <template #title>{{ item.title }}</template>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { useRoute } from 'vue-router';
import {useSidebarStore} from "@/store/sidebar";
const sidebar = useSidebarStore();

const items = JSON.parse(localStorage.getItem("roleMenu"));

const route = useRoute();
const onRoutes = computed(() => {
    return route.path;
});

</script>

<style scoped>
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
    width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
    width: 200px;
}
.sidebar > ul {
    height: 100%;
}
</style>
